# Written by koya
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

import random, image

class particle3d_obj( object ):
    def __init__( self ):
        
        self.life = 1.0														
        self.fade = 0.0
		#self.color = random.choice( [(1.0,0.5,0.5),(1.0,0.75,0.5),(1.0,1.0,0.5),(0.75,1.0,0.5),\
	    #(0.5,1.0,0.5),(0.5,1.0,0.75),(0.5,1.0,1.0),(0.5,0.75,1.0),\
	    #(0.5,0.5,1.0),(0.75,0.5,1.0),(1.0,0.5,1.0),(1.0,0.5,0.75) ] )
        self.color = [ 1.0, 1.0, 1.0 ]
		#self.velocity_x = 0.0
		#self.velocity_y = 0.0 #float( random.randrange( 0, 50 )-25.0)*10.0
		#self.velocity_z = 0.0
        self.velocity = [ 0.0, 0.0, 0.0 ]
        self.accelerate = [ 0.0, 0.0, 0.0 ]
		#self.accelerate_x = 0.0								
		#self.accelerate_y = 0.0 #-0.8
		#self.accelerate_z = 0.0

        self.position = [ 0.0, 0.0, 0.0 ]

class particle_system_obj( object ):

    def __init__( self, particle_num, particle_size ):

        self.particle_texid = [0]
        self.particle_texid = image.load_texture2( "data/particle/Cloud.bmp")
        
        self.size = particle_size

        self.particles = []
        
        for i in xrange( particle_num ):

            particle = particle3d_obj()
            particle.life = 1.0
            particle.fade = float(random.randrange( 0, 100 ))/1000.0 + 0.005
            particle.color = random.choice( [(1.0,0.5,0.5),(1.0,0.75,0.5),(1.0,1.0,0.5),(0.75,1.0,0.5),\
	           (0.5,1.0,0.5),(0.5,1.0,0.75),(0.5,1.0,1.0),(0.5,0.75,1.0),\
	           (0.5,0.5,1.0),(0.75,0.5,1.0),(1.0,0.5,1.0),(1.0,0.5,0.75) ] )

            particle.velocity = [ float( random.randrange( 0, 50 )-26.0)/100.0,\
                                float( random.randrange( 0, 50 )-26.0)/100.0, 0.0 ]
            particle.accelerate = [ 0.0, 0.0, 0.005 ]
            particle.position = [ 0.0, 0.0, -100.0 ]

            self.particles.append( particle )
	
    def render( self ):
        
        for particle in self.particles:

            
            glEnable( GL_BLEND )
            glBlendFunc(GL_SRC_ALPHA,GL_ONE)
            glEnable( GL_TEXTURE_2D )
            glBindTexture( GL_TEXTURE_2D, self.particle_texid )
            #glDepthMask( GL_TRUE )
            glPushMatrix()

            glTranslatef( particle.position[0], particle.position[1], particle.position[2] )
            glColor4f( particle.color[0], particle.color[1], particle.color[2], particle.life )
            
            
            glBegin(GL_QUADS)
            glTexCoord2f(0.0, 1.0 )
            glVertex3f(- self.size, self.size, 0)
            glTexCoord2f(0.0, 0.0)
            glVertex3f(- self.size, -self.size, 0)
            glTexCoord2f(1.0, 0.0)
            glVertex3f( self.size, -self.size, 0)
            glTexCoord2f(1.0, 1.0 )
            glVertex3f( self.size, self.size, 0)
            glEnd()

            #glDepthMask( GL_TRUE )
            #glDisable( GL_BLEND )
            glDisable( GL_TEXTURE_2D )

            particle.velocity[0] += particle.accelerate[0]
            particle.velocity[1] += particle.accelerate[1]
            particle.velocity[2] += particle.accelerate[2]

            particle.position[0] += particle.velocity[0]
            particle.position[1] += particle.velocity[1]
            particle.position[2] += particle.velocity[2]

            particle.life -= particle.fade
            
            
            if particle.life < 0:

                particle.life = 1.0
                particle.fade = float(random.randrange( 0, 100 ))/1000.0 + 0.005
                particle.color = random.choice( [(1.0,0.5,0.5),(1.0,0.75,0.5),(1.0,1.0,0.5),(0.75,1.0,0.5),\
                   (0.5,1.0,0.5),(0.5,1.0,0.75),(0.5,1.0,1.0),(0.5,0.75,1.0),\
                   (0.5,0.5,1.0),(0.75,0.5,1.0),(1.0,0.5,1.0),(1.0,0.5,0.75) ] )
                particle.velocity = [ float( random.randrange( 0, 50 )-26.0)/100.0,\
                                    float( random.randrange( 0, 50 )-26.0)/100.0, 0.0 ]
                particle.accelerate = [ 0.0, 0.0, 0.05 ]
                particle.position = [ 0.0, 0.0, -100.0 ]

            
            glPopMatrix()

            
        

        glColor4f(0,0,0,1)
        #pygame.display.flip()